<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
 
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
 
/**
 * HelloWorld Model
 */
class TintucModelTintuc extends JModelItem
{
         /**
         * @var array messages
         */
        protected $messages;
 
        /**
         * Returns a reference to the a Table object, always creating it.
         *
         * @param       type    The table type to instantiate
         * @param       string  A prefix for the table class name. Optional.
         * @param       array   Configuration array for model. Optional.
         * @return      JTable  A database object
         * @since       2.5
         */
        public function getTable($type = 'TinTuc', $prefix = 'TinTucTable', $config = array()) 
        {
                return JTable::getInstance($type, $prefix, $config);
        }
        /**
         * Get the message
         * @param  int    The corresponding id of the message to be retrieved
         * @return string The message to be displayed to the user
         */
        public function getMsg($id = 1) 
        {
                if (!is_array($this->messages))
                {
                        $this->messages = array();
                }
 
                if (!isset($this->messages[$id])) 
                {
                        //request the selected id
                        $jinput = JFactory::getApplication()->input;
                        $id = $jinput->get('id', 1, 'INT' );
 
                        // Get a TableHelloWorld instance
                        $table = $this->getTable();
 
                        // Load the message
                        $table->load($id);
 
                        // Assign the message
                        $this->messages[$id] = $table->greeting;
                }
				return "abc";
                return $this->messages[$id];
        }
		
		function getItem($id,$catId){ //lay thong tin chi tiet cua mot article
			global $mainframe;
			$db = JFactory::getDBO();
			$query = 'SELECT * FROM #__content WHERE id='.$id.' AND state=1';
			$db->setQuery($query);
			$item = $db->loadObject();
			// $fullArticle = $db->loadResult();
			// print_r($fullArticle);
			// die;
			$attribs = json_decode($item->attribs, true);
			$data['item'] = $item;
			$data['attribs'] = $attribs;
			
			//get next/prev link article
			$query = "SELECT * FROM #__content WHERE catid ='" . $catId . "'";
			$db->setQuery( $query );
            $list = $db->loadObjectList();
			// location of current content item in array list
            $prev    = null;
            $current = array_shift( $list );
            $next    = array_shift( $list );
            while ( $current->id != $id ) {
                $prev    = $current;
                $current = $next;
                $next    = array_shift( $list );
            }
            $data['prev'] = '';
            $data['next'] = '';
            if ( !empty( $prev ) ) {
                $data['prev']      = $prev->id;
                // $prev_title = $prev->title;
            }
            if ( !empty( $next ) ) {
                $data['next']       = $next->id;
                // $next_title = $next->title;
            }
			return $data;
		}
		
		function getCatID(){
			global $mainframe;
			$db = JFactory::getDBO();
			$query = 'SELECT * FROM #__tintuc WHERE id=1';
			$db->setQuery($query);
			$item = $db->loadObject();
			return $item->cat_id;
		}
		
		function getItems($catId = 0,$limit = 4, $prefix){
			global $mainframe;
			$db			=JFactory::getDBO();
			// Create the pagination object
			
			$limitstart=JRequest::getCmd('limitstart','0');
			$limitstart = ( $limit != 0 ? (floor(($limitstart) / $limit) * $limit) : 0 );
			$query = "SELECT count(id) FROM #__content WHERE catid ='" . $catId . "'";

			$db->setQuery( $query );
			$total = $db->loadResult();

			jimport('joomla.html.pagination');
			$pagination = new JPagination($total, $limitstart, $limit, $prefix);
			
			$query = "SELECT * FROM #__content WHERE catid ='" . $catId . "'";
			$db->setQuery($query, $pagination->limitstart, $pagination->limit);
			
			//$db->setQuery($query); 
			$data['rows'] = $db->loadObjectList(); 
			// $data['rows'] = $db->loadAssocList(); 
			$data['pagination'] = $pagination;
			return $data;
		}
}